package jmine.tec.test.junit3;

import jmine.tec.test.utils.legacy.hibernate.HibernateUtils;

import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 * @author takeshi
 * @deprecated
 */
@Deprecated
public abstract class EnhancedTestCaseHelper extends EnhancedTestCase {

    private String config;

    /**
     * @param resource Path to hibernate cfg file
     */
    public EnhancedTestCaseHelper(final String resource) {
        this.config = resource;
    }

    private Session session;

    private Transaction tx;

    /**
     * @throws Exception e
     */
    @Override
    protected final void setUpBeforeClass() throws Exception {
        super.setUpBeforeClass();
        HibernateUtils.configure(this.config);
    }

    /**
     * @throws Exception e
     */
    @Override
    protected void setUpBeforeTest() throws Exception {
        super.setUpBeforeTest();
        this.session = HibernateUtils.getSession();
        this.tx = this.session.beginTransaction();
    }

    /**
     * @throws Exception e
     */
    @Override
    protected void tearDownAfterTest() throws Exception {
        if (this.tx.isActive()) {
            this.tx.rollback();
        }
        if (this.session.isOpen()) {
            HibernateUtils.closeSession();
        }
        super.tearDownAfterTest();
    }

    /**
     * @throws Exception e
     */
    @Override
    protected final void tearDownAfterClass() throws Exception {
        HibernateUtils.closeSession();
        HibernateUtils.closeFactory();
        super.tearDownAfterClass();
    }

    /**
     * @return the session
     */
    public Session getSession() {
        return this.session;
    }
}
